//
//  RCChangePictureView.m
//  RC_Modern_Home
//
//  Created by Marx on 15/10/22.
//  Copyright © 2015年 RC. All rights reserved.
//

#import "GJFCarouselFigurView.h"
#import "GJFBannerModel.h"

typedef enum : NSUInteger {
    kGJFDirectFromLeft,
    kGJFDirectFromRight,
} kGJFDirect;

@interface GJFCarouselFigurView ()
@property (nonatomic, retain)NSArray *dataArr;
@property (nonatomic, retain)UIImageView *imageV;
@property (nonatomic, retain)UIPageControl *pageControl;
@property (nonatomic, assign)NSInteger flag;
@property (nonatomic, retain)NSTimer *timer;
@end

@implementation GJFCarouselFigurView

#pragma mark - 网络请求下来的图片数组传入轮播图
- (void)refreshScrollView:(NSArray *)imageArr {
    self.dataArr = imageArr;
    self.flag = 0;
    // 判断定时器对象存在不？或者 定时器是否可用 valid(可用)
    if (!self.timer || !self.timer.isValid) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(autoPicture) userInfo:nil repeats:YES];
    }
    [self.timer fire];
}

- (void)autoPicture {
    [self leftAction];
}

#pragma mark - 动画来的方向与轻扫手势方向相反
- (void)leftAction {
    // 切换图片+动画
    [self changeImageWithDirect:kGJFDirectFromRight];
}
- (void)rightAction {
    // 切换图片+动画
    [self changeImageWithDirect:kGJFDirectFromLeft];
}

#pragma mark - 更换图片（切换过程中，增加动画） next:YES
- (void)changeImageWithDirect:(kGJFDirect)direct {
    // 创建CA动画
    CATransition *transition = [CATransition animation];
    transition.duration = 1; // 动画时长
    transition.repeatCount = 1; // 动画重复次数
    transition.type = @"cube"; // 立方体动画
    if (kGJFDirectFromRight == direct) {
        transition.subtype = kCATransitionFromRight; // 旋转方向-左
    }
    else {
        transition.subtype = kCATransitionFromLeft; // 旋转方向-右
    }
    GJFBannerModel *m = [self getImageModelWithDirect:direct];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:m.image_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
    //给图片添加立体动画
    [self.imageV.layer addAnimation:transition forKey:@"qw"];
    self.pageControl.currentPage = self.flag;
}

#pragma mark - 根据方向计算应该显示数组中哪一张图片
- (GJFBannerModel *)getImageModelWithDirect:(kGJFDirect)direct {
    if (kGJFDirectFromRight == direct) {
        self.flag++;
        if (self.flag == self.dataArr.count) {
            self.flag = 0;
        }
    }
    else {
        self.flag--;
        if (self.flag < 0) {
            self.flag = self.dataArr.count - 1;
        }
    }
    return self.dataArr[self.flag];
}

#pragma mark - 懒加载

// 相框加轻扫手势
- (UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:self.bounds];
        _imageV.userInteractionEnabled = YES;
        _imageV.contentMode = UIViewContentModeScaleAspectFill;
        _imageV.clipsToBounds = YES;
        [self addSubview:_imageV];
        UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftAction)];
        left.direction = UISwipeGestureRecognizerDirectionLeft;
        [_imageV addGestureRecognizer:left];
        UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightAction)];
        right.direction = UISwipeGestureRecognizerDirectionRight;
        [_imageV addGestureRecognizer:right];
    }
    return _imageV;
}

- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30 * YY)];
        _pageControl.center = CGPointMake(self.center.x, 0);
        _pageControl.frame = CGRectMake(_pageControl.frame.origin.x, self.frame.size.height - 30 * YY, _pageControl.frame.size.width, _pageControl.frame.size.height);
        _pageControl.numberOfPages = self.dataArr.count;
        [self addSubview:_pageControl];
    }
    return _pageControl;
}

#pragma mark - 触碰停止自动滚动（自带）
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.timer invalidate];
    NSLog(@"Start");
}
#pragma mark - 抬起自动滚动-并且做出响应时间（自带）
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"End");
    if (_delegate && [_delegate respondsToSelector:@selector(clickCarouselFigurViewWithIndex:)]) {
        [_delegate clickCarouselFigurViewWithIndex:self.flag];
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(autoPicture) userInfo:nil repeats:YES];
}
#pragma mark - 取消自动滚动（自带）
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"cancel");
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(autoPicture) userInfo:nil repeats:YES];
}

@end
